#!/usr/bin/env bash

cpu=`mpstat 1 1| awk 'NR==5{print int(100-$12)}'`
mem=`free | awk 'NR==2{print int($3/$2 * 100)}'`
disk=`df . | awk 'NR==2{print int($5)}'`

echo cpu: $cpu% , mem: $mem% , disk: $disk%

if [ "$cpu" -lt 80 ] && [ "$mem" -lt 90 ] && [ "$disk" -lt 80 ]; then
 exit 0;
fi


log="/app/sys-log/monitor-`date "+%Y-%m-%d"`.log" 
echo "---------------------------------------------------"  >> $log
echo "[时间]: "`date "+%Y-%m-%d %H:%M:%S"` >> $log
echo "[CPU占用]: $cpu%"   >> $log
echo "[内存占用]: $mem%"  >> $log
echo "[磁盘占用]: $disk%" >> $log

echo "[CPU占用前5的进程]:"  >> $log
ps -eo pid,%cpu,%mem,cmd --sort=-%cpu | head -n 5  >> $log

echo "[内存占用前5的进程]:" >> $log
ps -eo pid,%cpu,%mem,cmd --sort=-%mem | head -n 5 >> $log
